package com.duoduo.${serviceName}.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.PageRequest;
import com.duoduo.common.core.util.R;
import com.duoduo.${serviceName}.api.dto.${entityType}DTO;
import com.duoduo.${serviceName}.service.${entityType}Service;
import org.springframework.http.HttpHeaders;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("${entity}")
@Tag(name = "${entityModel}接口", description ="${entityModel}访问接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@RequiredArgsConstructor
public class ${entityType}Controller {
	
	private final ${entityType}Service ${entity}Service;
	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Operation(summary = "分页")
	@PreAuthorize("@pms.hasPermission('${permission}_page')")
	public R<Page<${entityType}DTO>> page(@RequestBody PageRequest<${entityType}DTO> request){
		Page<${entityType}DTO> page = ${entity}Service.page(request.getPageNum(),request.getPageSize(),request.getParam());
		return R.ok(page);
	}
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/get")
	@Operation(summary = "详情")
	@PreAuthorize("@pms.hasPermission('${permission}_get')")
	public R<${entityType}DTO> get(@Parameter(description = "主键id") @PathVariable("id")Long id){
		return R.ok(${entity}Service.get(id));
	}
	/**
	 * 创建
	 * @param id
	 * @return
	 */
	@Operation(summary = "创建")
	@PostMapping("create")
	@PreAuthorize(@RequestBody "@pms.hasPermission('${permission}_create')")
	public R<Void> create(@RequestBody ${entityType}DTO request){
		${entity}Service.create(request);
		return R.ok();
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@Operation(summary = "修改")
	@PutMapping("update")
	@PreAuthorize("@pms.hasPermission('${permission}_update')")
	public R<Void> update(@RequestBody ${entityType}DTO request){
		${entity}Service.update(request,request.getId());
		return R.ok();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除")
	@DeleteMapping("{id}/delete")
	@PreAuthorize("@pms.hasPermission('${permission}_delete')")
	public R<Void> delete(@Parameter(description = "主键id") @PathVariable("id")Long id){
		${entity}Service.delete(id);
		return R.ok();
	}
}
